package com.itheima.controller;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result2;
import com.itheima.pojo.Dto.MemberDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 手机快速登录接口
 */
@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result2 login(@RequestBody MemberDto memberDto) {
        log.info("手机快速登录：{}", memberDto);
        String code = (String) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_LOGIN + memberDto.getTelephone());
        if (code != null && code.equals(memberDto.getValidatecode())) {
            // 登录成功
            // 删除Redis中的验证码
            redisTemplate.delete(RedisMessageConstant.SENDTYPE_LOGIN + memberDto.getTelephone());
        } else {
            // 登录失败
            return new Result2(false, "登录失败");
        }
        return new Result2(true, "登录成功");
    }
}
